package com.hair.logy.core.controller;

import com.hair.logy.core.model.HairCard;
import com.hair.logy.core.model.HairCardInfo;
import com.hair.logy.core.model.ResultVo;
import com.hair.logy.core.service.HairCardService;
import com.hair.logy.core.utils.BusinessException;
import com.hair.logy.core.utils.QRcodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class CoderController {



    @Autowired
    private HairCardService hairCardService;

    @GetMapping("/hair-card")
    public ResultVo<HairCardInfo> listHairCard(@RequestParam(value = "start", required = false)Integer start, @RequestParam(value = "end" ,required = false) Integer end, @RequestParam(value = "cardNo" ,required = false) String  cardNo){
        ResultVo resultVo=new ResultVo();
        //生成一定数量的二维码信息


        if(start==null&&end==null){
            start=0;
            end=10;
        }
        HairCardInfo  hairCards   =hairCardService.listHairCard(start,end,cardNo);
        resultVo.setCode(0);
        resultVo.setData(hairCards);
       return  resultVo;
    }


    @GetMapping("code-with-logo")
    public void qrcodeWithLogo(String url, HttpServletResponse response) throws Exception {
        QRcodeUtil.encode(url, response);
    }
    // 下载文件
    @GetMapping("/downLoadFile")
    public void downLoadFile(@RequestParam(value = "number" ,required = false )Integer number, HttpServletResponse response) throws Exception {
        if(number==null||number<=0){
            number=10;
        }
        hairCardService.generateQrcode(number ,response);
    }


    @GetMapping("/downLoadHair")
    public void downLoadHairFile(@RequestParam(value = "cardNo" ,required = true )String cardNo, HttpServletResponse response) throws Exception {

        hairCardService.downLoadHairFile(cardNo ,response);
    }


    @PostMapping("/register")
    public ResultVo register(@RequestBody HairCard  hairCard) throws Exception {
        ResultVo  resultVo=new ResultVo();
        if(hairCard.getUserName()==null||"".equals(hairCard.getUserName())){
            resultVo.setCode(-1);
            resultVo.setMsg("姓名不能为空");
            return resultVo;
        }
        if(hairCard.getIphoneNo()==null||"".equals(hairCard.getIphoneNo())){
            resultVo.setCode(-1);
            resultVo.setMsg("手机不能为空");
            return resultVo;
        }
        if(hairCard.getCardNo()==null){
            resultVo.setCode(-1);
            resultVo.setMsg("卡号不能为空");
            return resultVo;
         }
        try {
            hairCardService.register(hairCard);
            resultVo.setCode(0);
            resultVo.setMsg("注册成功");
        }catch (BusinessException  e){
            System.out.println(e.getMsg());
            resultVo.setCode(-1);
            resultVo.setMsg(e.getMsg());
        }
         return resultVo;
    }



    @PutMapping ("/activate")
    public ResultVo activateHairCard(@RequestParam("id") Long  id) throws Exception {
        ResultVo  resultVo=new ResultVo();
        try {
            HairCard   hairCards = hairCardService.activateHairCard(id);
            resultVo.setData(hairCards);
            resultVo.setCode(0);
            resultVo.setMsg("激活成功");
        }catch (BusinessException  e){
            resultVo.setCode(-1);
            resultVo.setMsg(e.getMsg());
        }
        return resultVo;
    }
}
